
package progappsproject1;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author jordan
 */
public class GenerateReport extends javax.swing.JFrame {

    /**
     * Creates new form GenerateReport
     */
    public GenerateReport() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        studentList = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        studentList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "LATE STUDENTS", "ABSENT STUDENTS", "STUDENTS TO BE DROPPED"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                true, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(studentList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 584, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GenerateReport.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GenerateReport.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GenerateReport.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GenerateReport.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GenerateReport().setVisible(true);
            }
        });
    }
    
    
    void showReport(){
            try {
                Class.forName("com.mysql.jdbc.Driver");
                String url = "jdbc:mysql://localhost:3306/progapps";
                String name = "root";
                String password = "brizzalix";
                try {
                    
                    java.sql.Connection con = DriverManager.getConnection(url, name, password);
                    java.sql.Statement stmts1 = null;
                    java.sql.Statement stmts2 = null;
                    java.sql.Statement stmts3 = null;

                    String queryLateStudents = "select concat(lastname,', ',firstname,'  ',middleinitial) as lateStudents from studentinfo where status = 'late' ";
                    String queryAbsentStudents = "select concat(lastname,', ',firstname,'  ',middleinitial) as absentStudents from studentinfo where status = 'absent' ";
                    String queryDropedStudents = "select concat(lastname,', ',firstname,'  ',middleinitial) as dropStudents from studentinfo where numberofabsents >= 5";
                    stmts1 = con.createStatement();
                    stmts2 = con.createStatement();
                    stmts3 = con.createStatement();

                    stmts2.executeUpdate("update studentinfo set numberofabsents=numberofabsents+1 where status = 'absent' ");
                    stmts1.executeUpdate("update studentinfo set numberoflates=numberoflates+1 where status = 'late' ");

                     ResultSet rs1 = stmts1.executeQuery(queryLateStudents);
                     ResultSet rs2 = stmts2.executeQuery(queryAbsentStudents);
                     ResultSet rs3 = stmts3.executeQuery(queryDropedStudents);
                     int row1 = 0;
                     int row2 = 0;
                     int row3 = 0;
                     rs1.next();
                     rs2.next();
                    while(rs1.next()){
                         studentList.setValueAt(rs1.getString("lateStudents"),row1,0);
                         row1++;
                     }

                    while(rs2.next()){
                         studentList.setValueAt(rs2.getString("absentStudents"),row2,1);
                         row2++;
                     }
                   while(rs3.next()){
                         studentList.setValueAt(rs3.getString("dropStudents"),row3,2);
                         row3++;
                     }

            
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
    
    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable studentList;
    // End of variables declaration                   
}
